package com.itheima.service.impl;

import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.FreeMarkerService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FreeMarkerServiceImpl implements FreeMarkerService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${free.out.path}")
    private String outPath;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 生成套餐列表的静态页面
     * @param fileName
     */
    @Override
    public void generateSetmealFile(String fileName) {
        // 获取配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            // 获取模板对象
            Template template = configuration.getTemplate(fileName + ".ftl");
            // 获取数据
            List<Setmeal> setmealList = setmealMapper.selectAll();
            if (setmealList == null || setmealList.isEmpty()){
                System.out.println("当前套餐为空");
                return;
            }
            // 创建数据模型
            Map<String, Object> dataModel = new HashMap<>();
            dataModel.put("setmealList", setmealList);
            // 生成静态页面
            File file = new File(outPath  + "setmeal.html");
            try(
                    // 创建输出流
                    Writer writer = new FileWriter(file)
                    ){
                // 处理模板并生成文件
                template.process(dataModel, writer);
            }catch (Exception e){
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成套餐详情的静态页面
     * @param fileName
     * @param id
     */
    @Override
    public void generateSetmealInfoFile(String fileName, Integer id) {
        // 获取配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            // 获取模板对象
            Template template = configuration.getTemplate(fileName + ".ftl");
            // 获取数据
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal == null){
                System.out.println("没有找到对应的套餐");
                return;
            }
            // 创建数据模型
            Map<String, Object> dataModel = new HashMap<>();
            dataModel.put("setmeal", setmeal);
            // 生成静态页面
            File file = new File(outPath  + "setmeal_detail_" + id + ".html");
            try(
                    // 创建输出流
                    Writer writer = new FileWriter(file)
            ){
                // 处理模板并生成文件
                template.process(dataModel, writer);
            }catch (Exception e){
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
